package com.foreknow.Test;

import com.foreknow.bean.Admin;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;

import java.sql.Connection;
import java.util.List;

/**
 * 测试DBManager以及JdbcTemplate
 */
public class TestDB {
    public static void main(String[] args) {
        //连接数据库
        DBManager dbManager = DBManager.getInstance();
        //调用连接数据库的方法
        Connection conn = dbManager.getConnection();
        //获取JdbcTemplate对象(单例)
        JdbcTemplate jdbcTemplate = JdbcTemplate.getInstance();
        //需要调用JdbcTemplate中的 setConnection（conn）方法来将DBManager中获取到的连接传递到JdbcTemplate，否则就会空指针异常（重要）
        jdbcTemplate.setConnection(conn);
        //调用查询的方法 public List<Object> query(String sql, EntityMapping mapping, Object...values)
        //第二个参数需要从MappingFactory工厂中获取到的AdminMapping对象
        //1.获取到工厂类的对象
        MappingFactory mappingFactory = MappingFactory.getInstance();
        //2.调用工厂类中的getMap(String key)方法，根据key值获取到AdminMapping对象
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.ADMIN_MAPPING);
        //获取数据
        List<Object> list = jdbcTemplate.query("select * from admin",mapping);
        //遍历集合
        for(int i = 0;i<list.size();i++){
            Admin admin = (Admin) list.get(i);
            System.out.println(admin.getId()+"--"+admin.getUsername()+"--"+admin.getPassword()+"--"+admin.getAge()+"--"+admin.getSchool());
        }
    }
}